﻿<UserControl x:Class="ModernUILogViewer.Content.ContentEntries"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:mui="http://firstfloorsoftware.com/ModernUI"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"                
             xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"                
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:view="clr-namespace:ModernUILogViewer.View"
             xmlns:conv="clr-namespace:ModernUILogViewer.Converters"
             xmlns:business="clr-namespace:ModernUILogViewer.Business"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             >

    <UserControl.Resources>        
        <conv:NullToBooleanConverter x:Key="nulltobooleanconverter" />
        <business:DataContextSpy x:Key="spy" />                    
        
        <Style x:Key="borderStyle" TargetType="{x:Type Border}">
            <Setter Property="Width" Value="10" />
            <Setter Property="Height" Value="10" />
            <Setter Property="Margin" Value="5, 0, 0, 0" />
            <Setter Property="CornerRadius" Value="2, 2, 2, 2" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding}" Value="NONE">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=NoneColor, TargetNullValue={StaticResource NONE_COLOR}}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding}" Value="DEBUG">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=DebugColor, TargetNullValue={StaticResource DEBUG_COLOR}}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding}" Value="INFO">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=InfoColor, TargetNullValue={StaticResource INFO_COLOR}}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding}" Value="WARN">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=WarnColor, TargetNullValue={StaticResource WARN_COLOR}}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding}" Value="ERROR">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=ErrorColor, TargetNullValue={StaticResource ERROR_COLOR}}"></Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding}" Value="FATAL">
                    <Setter Property="Background" Value="{Binding Source={StaticResource colorsettings}, Path=FatalColor, TargetNullValue={StaticResource FATAL_COLOR}}"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.Resources>
            <conv:ConverterChain x:Key="nulltovisibilityconverter">
                <conv:NullToBooleanConverter />
                <BooleanToVisibilityConverter />
            </conv:ConverterChain>            
        </Grid.Resources>
        <DockPanel>            
            <Border DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" Margin="10, 0, 10, 10" BorderBrush="{DynamicResource ButtonBorder}" BorderThickness="1">                
                <TextBlock FontWeight="SemiBold" Margin="4" HorizontalAlignment="Center">
                    <Run Text="Entries: " />
                    <Run Text="{Binding FilteredLogEntries.Count, Mode=OneWay}" />                
                    <Run Text=" / " />
                    <Run Text="{Binding EntriesCount, Mode=OneWay}" />
                </TextBlock>
            </Border>
            <DataGrid x:Name="dataGrid" ItemsSource="{Binding FilteredLogEntries, Mode=OneWay}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedLogEntry, Mode=TwoWay}" IsReadOnly="True" DockPanel.Dock="Top" Margin="10, 0, 10, 10">                
                <DataGrid.Resources>
                    <conv:IsEqualOrGreaterThanConverter x:Key="equalgreaterconverter" />
                </DataGrid.Resources>
                <DataGrid.Style>                                        
                    <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">                        
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding ListFiles, Converter={StaticResource nulltobooleanconverter}}" Value="True" />
                                    <Condition Binding="{Binding EntriesCount, Converter={StaticResource equalgreaterconverter}, ConverterParameter='1'}" Value="True" />
                                </MultiDataTrigger.Conditions>
                                <Setter Property="Visibility" Value="Visible" />
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Style>
                <DataGrid.ContextMenu>
                    <ContextMenu>                        
                        <MenuItem Header="Copy log message to clipboard" 
                                  Command="{x:Static view:ViewCommands.CopyTextToClipboard}" CommandParameter="{Binding SelectedLogEntry.Message}" 
                                  IsEnabled="{Binding SelectedLogEntry, Converter={StaticResource nulltobooleanconverter}, ConverterParameter='false'}"
                                  >
                            <MenuItem.Icon>
                                <Path Fill="#FF2B2B2B"
                                      Width="14.3775"
                                      Height="14"
                                      Canvas.Left="0.311264"
                                      Canvas.Top="0.500005"
                                      Stretch="Fill"
                                      Data="{StaticResource copyPathData}"                                       
                                      />                                
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Copy line to clipboard" 
                                  Command="{x:Static view:ViewCommands.CopyTextToClipboard}" CommandParameter="{Binding SelectedLogEntry.Line}" 
                                  IsEnabled="{Binding SelectedLogEntry, Converter={StaticResource nulltobooleanconverter}, ConverterParameter='false'}"
                                  >
                            <MenuItem.Icon>
                                <Path Fill="#FF2B2B2B"
                                      Width="14.3775"
                                      Height="14"
                                      Canvas.Left="0.311264"
                                      Canvas.Top="0.500005"
                                      Stretch="Fill"
                                      Data="{StaticResource linePathData}"
                                      />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Open folder location" 
                                  Command="{x:Static view:ViewCommands.OpenFolderLocation}" 
                                  IsEnabled="{Binding SelectedLogEntry, Converter={StaticResource nulltobooleanconverter}, ConverterParameter='false'}"
                                  >
                            <MenuItem.CommandParameter>
                                <Binding Path="SelectedLogEntry.FileIndex" Mode="OneWay">
                                    <Binding.Converter>                                        
                                        <conv:IndexToFilenameConverter Files="{Binding Source={StaticResource spy}, Path=DataContext.ListFiles, Mode=OneWay}" />
                                    </Binding.Converter>
                                </Binding>
                            </MenuItem.CommandParameter>
                            <MenuItem.Icon>
                                <Path Fill="#FF2B2B2B"
                                      Width="14.3775"
                                      Height="14"
                                      Canvas.Left="0.311264"
                                      Canvas.Top="0.500005"
                                      Stretch="Fill"
                                      Data="{StaticResource openFolderPathData}"
                                      />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Open with external program" 
                                  Command="{Binding OpenWithExternalEditor}"                                   
                                  >
                            <MenuItem.Style>
                                <Style TargetType="{x:Type MenuItem}">
                                    <Setter Property="IsEnabled" Value="False" />
                                    <Style.Triggers>
                                        <MultiDataTrigger>
                                            <MultiDataTrigger.Conditions>
                                                <Condition Binding="{Binding SelectedLogEntry, Converter={StaticResource nulltobooleanconverter}, ConverterParameter='false'}" Value="True" />
                                                <Condition Binding="{Binding Source={StaticResource mainVM}, Path=ExternalEditorVM.ExePath,  Converter={StaticResource nulltobooleanconverter}, ConverterParameter='False'}" Value="True" />
                                            </MultiDataTrigger.Conditions>
                                            <Setter Property="IsEnabled" Value="True" />
                                        </MultiDataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </MenuItem.Style>
                            <MenuItem.CommandParameter>
                                <Binding Path="SelectedLogEntry" Mode="OneWay" />                                    
                            </MenuItem.CommandParameter>
                            <MenuItem.Icon>
                                <Path Width="14.3775"
                                      Height="14"
                                      Canvas.Left="21" 
                                      Canvas.Top="15.8333" 
                                      Stretch="Fill" 
                                      Fill="#FF000000" 
                                      Data="{StaticResource openExecutablePathData}"
                                      />
                            </MenuItem.Icon>
                        </MenuItem>                        
                    </ContextMenu>
                </DataGrid.ContextMenu>
                <DataGrid.Columns>
                    <DataGridTemplateColumn>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Border Style="{StaticResource borderStyle}" DataContext="{Binding Level}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="TIME" Binding="{Binding DTime, StringFormat='dd-MM-yyyy HH:mm:ss', Mode=OneTime}" />
                    <DataGridTextColumn Header="LEVEL" Binding="{Binding Level, Mode=OneTime}"  />
                    <DataGridTextColumn Header="LINE" Binding="{Binding Line, Mode=OneTime}"  />
                    <DataGridTextColumn Header="FILE">
                        <DataGridTextColumn.Binding>                            
                            <Binding Path="FileIndex" Mode="OneTime">
                                <Binding.Converter>                                    
                                    <!--<conv:IndexToFilenameConverter Files="{Binding Source={StaticResource spy}, Path=DataContext.ListFiles}" />-->
                                    <conv:ConverterChain>
                                        <conv:IndexToFilenameConverter Files="{Binding Source={StaticResource spy}, Path=DataContext.ListFiles}" />
                                        <conv:FilenameConverter />                                        
                                    </conv:ConverterChain>
                                </Binding.Converter>
                            </Binding>
                        </DataGridTextColumn.Binding>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>            
        </DockPanel>
        <StackPanel HorizontalAlignment="Center" Visibility="{Binding ListFiles, Converter={StaticResource nulltovisibilityconverter}, ConverterParameter='True'}">
            <TextBlock HorizontalAlignment="Center">No log files loaded</TextBlock>
            <TextBlock  HorizontalAlignment="Center" Margin="0, 5, 0, 0">
                <Hyperlink Command="{Binding Open}">Click here</Hyperlink>
            </TextBlock>
        </StackPanel>
        <StackPanel HorizontalAlignment="Center">            
            <StackPanel.Style>                
                <Style TargetType="{x:Type StackPanel}">
                    <Setter Property="Visibility" Value="Collapsed" />
                    <Style.Triggers>                        
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>                                
                                <Condition Binding="{Binding ListFiles, Converter={StaticResource nulltobooleanconverter}}" Value="True" />
                                <Condition Binding="{Binding EntriesCount}" Value="0" />
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Visibility" Value="Visible" />
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </StackPanel.Style>
            <TextBlock HorizontalAlignment="Center">No items found</TextBlock>
            <mui:BBCodeBlock HorizontalAlignment="Center" BBCode="[url=/Pages/RegexSettings.xaml]Go to configuration[/url]" />
        </StackPanel>
    </Grid>

</UserControl>
